$(function () {
    var initTopicCategoryUrl = '/topic/getTopicCategory';
    var submitTopicUrl = '/topic/publishTopic';
    var postType = 1;
    var head = getQueryString('head');


    //渲染话题类型
    getTopicCategory();

    //判断是上传图片还是上传视频
    $('#type').change(function () {
        var type = $(this).find('option').not(
            function () {
                return !this.selected;
            }).data('id');
        postType = type;
        if (type == '1') {
            $('#image').show();
            $('#video').hide();
        }else {
            $('#image').hide();
            $('#video').show();
        }
    });


    $('#submit').click(function () {
        var topic = {};
        topic.topicName=$('#topic-name').val();
        topic.topicDesc=$('#topic-desc').val();
        //获取话题的类别
        topic.topicCategoryId = $('#topic_category').find('option').not(
                function () {
                    return !this.selected;
                }).data('id');
        topic.topicCategoryName = $('#topic_category').find('option').not(
            function () {
                return !this.selected;
            }).data('name');
        var formData = new FormData();
        formData.append("type",postType);
        if (postType == '1'){
            //遍历帖子图片控件,获取里面的文件流
            $('.detail-img').map(function (index,item) {
                //判断该控件是否已选择了文件
                if ($('.detail-img')[index].files.length > 0) {
                    //将第i个文件流赋值给key为productImg的表单键值对里
                    formData.append("topicImg"+index,
                        $('.detail-img')[index].files[0]);
                }
            });
        } else {
            //获取视频
            formData.append("video",$('#videoFile')[0].files[0]);
        }

        //将topic json对象转成字符流保存至表单对象key为topicStr的键值对里
        formData.append('topicStr',JSON.stringify(topic));
        formData.append("token",localStorage.getItem('token'));
        formData.append('head',head);
        //将数据提交至后台处理相关操作
        $.ajax({
            url:submitTopicUrl,
            type:'post',
            data:formData,
            contentType:false,
            processData:false,
            cache:false,
            beforeSend: function(request){
                request.setRequestHeader("Token",localStorage.getItem('token'));
            },
            success:function (data) {
                console.log("error:"+data);
                $.toast(data.message);
                if (data.success&&!head) {
                    var url = '/topic/topicList';
                    setTimeout("window.location.href = '" + url + "'",2000);
                }
            }
        })

    });


    //获取话题类型并添加到指定选项
    function getTopicCategory() {
        $.getJSON(initTopicCategoryUrl,function (data) {
            if (data.success){
                var topic = data.data;
                var tempTopicHtml = '';
                topic.map(function (item,index) {
                    tempTopicHtml += '<option data-id="'+item.topicCategoryId+'" data-name="'+item.topicCategoryName+'">'
                        +item.topicCategoryName+'</option>';
                });
                $('#topic_category').html(tempTopicHtml);
            }
        });
    }

    //针对话题详情图控件组,若该控件组的最后一个元素发生变化(即上传了图片)
    //且控件总数未达到6个,则生成新的一个文件上传控件
    $('.detail-img-div').on('change','.detail-img:last-child',function () {
        if ($('.detail-img').length<6){
            $('#detail-img').append('<input type="file" class="detail-img">');
        }
    });

});
